package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.entity.Audit;
import com.czz.manage.entity.Teacher;
import com.czz.manage.entity.User;
import com.czz.manage.mapper.AuditMapper;
import com.czz.manage.mapper.TeacherMapper;
import com.czz.manage.mapper.UserMapper;
import com.czz.manage.query.AuditQuery;
import com.czz.manage.service.AuditService;
import com.czz.manage.vo.AuditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: AuditServiceImpl
 * @Author Czz
 * @Date 2020/1/8
 * @Time 10:05
 **/
@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> implements AuditService {

    @Autowired
    AuditMapper auditMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<AuditVo> getAuditVoPageList(Page<AuditVo> pageParam, AuditQuery auditQuery) {
        return auditMapper.getAuditVoPageList(pageParam,auditQuery);
    }

    @Override
    @Transactional
    public void updateAuditStatusById(AuditVo auditVo) {
        Audit audit = new Audit();
        audit.setAuditId(auditVo.getAuditId());
        audit.setOpinion(auditVo.getOpinion());
        audit.setStatus(auditVo.getStatus());

        //审核通过，将数据添加到teacher表，并且将修改user表的is_teacher字段和audit表的status字段
        if (auditVo.getStatus() == 1){
            Audit auditTemp = auditMapper.selectById(auditVo.getAuditId());

            Teacher teacher = new Teacher();
            teacher.setUserId(auditTemp.getUserId());
            teacher.setName(auditTemp.getName());
            teacher.setIntro(auditTemp.getIntro());
            teacher.setCareer(auditTemp.getCareer());

            User user = new User();
            user.setId(auditVo.getUserId());
            user.setTeacher(1);

            teacherMapper.insert(teacher);
            userMapper.updateById(user);
            auditMapper.updateById(audit);
            return;
        }
        //审核不通过，只修改audit表的status
        else {
            auditMapper.updateById(audit);
        }
    }

    @Override
    public void updateAuditById(AuditVo auditVo) {
        Audit audit = new Audit();
        audit.setAuditId(auditVo.getAuditId());
        audit.setName(auditVo.getName());
        audit.setSort(auditVo.getSort());
        audit.setIntro(auditVo.getIntro());
        audit.setCareer(auditVo.getCareer());
        auditMapper.updateById(audit);
    }
}
